__metaclass__ = type

from lex import token
from noop import noop, reset

class atom:

    def __init__(self, token_type, value=None, func=noop, pre=reset, post=noop):
        self.token_type = token_type
        self.value = value
        self.func = func
        self.pre = pre
        self.post = post
        self.code = []
        self.children = []

    def __call__(self, value):
        ret = True
        token = value
        if value and type(value[0]) in [type(())]:
            token = value[0]
            ret = value[1:]
        token_type, token, (srow, scol), (erow, ecol), line = token
        if self.value is None:
            return token_type == self.token_type and (ret or True)
        return (token_type == self.token_type and
                token == self.value) and (ret or True)

    def __len__(self):
        return 1

    def __repr__(self):
        items = map(str, filter(None, [token.tok_name[self.token_type],
                                       self.value]))
        return '%s' % ' '.join(items)
